# -*- coding:utf-8 -*-
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import procDB
import os
'''
功能 ：发送邮件
参数：
    sender:     发送人
    receivers:   收件人列表,如['xxx@163.com', '111@21cn.com']
    subject:    主题
    atts:       附件列表，如['d:\out\test.txt', '']
'''
def sendMail(sender, receivers, subject, atts=[]):
    smtpserver = 'smtp.21cn.com'
    username = 'mgjdrobot'
    password = 'MgjdRobot1974'
    msgRoot = MIMEMultipart('related')
    msgRoot['from'] = "mgj<%s>" % sender
    msgRoot['to'] = ";".join(receivers)     #将收件人以‘;’分隔
    msgRoot['subject'] = subject
    #构造附件
    if atts:
        for attName in atts:
            att = MIMEText(open(attName, 'rb').read(), 'base64', 'utf-8')
            att['Content-Type'] = 'application/octet-stream'
            att['Content-Disposition'] = 'attachment; filename="%s"' % os.path.basename(attName)
            msgRoot.attach(att)
    try:
        smtp = smtplib.SMTP()
        smtp.connect(smtpserver)
        smtp.login(username, password)
        smtp.sendmail(sender, receivers, msgRoot.as_string())
        smtp.close()
        return True
    except Exception as e:
        print(str(e))
        return False

class Hotmail (object ):
    def __init__ (self ,account,password):
        self.account="%s@Hotmail.com" %account
        self.password=password

    def send (self ,to,title,content):
        #print self.account,self.password
        server = smtplib.SMTP('smtp.live.com' )
        ## server.set_debuglevel(1)
        server.docmd("EHLO server" )
        server.starttls()
        server.login(self.account,self.password)

        msg = MIMEText(content)
        msg['Content-Type' ]='text/plain; charset="utf-8"'
        msg['Subject' ] = title
        msg['From' ] = self.account
        msg['To' ] = to
        server.sendmail(self.account, to,msg.as_string())
        server.close()

def getAlarmlist():
    conn = procDB.get_conn('Alarm.db')
    sql = 'select '
    rows = procDB.fetchall(conn, sql)

